# 实现WSGI协议中的application接口方法
import re
from pymysql import connect

router_dict = {}
def application(envrion,start_response):
    """
    envrion-->字典，保存访问路径
    start_response -->用来回调，实现将web应用的处理结构回传给调用者
    返回值-->响应体，根据访问路径得到相应的值
    """
    url_path = envrion['PATH_INFO']
    #确定路由
    file_content = other()
    if url_path in router_dict:
        file_content = router_dict[url_path]()

    # 回调start_response函数，将响应状态信息回传给服务器
    start_response('200 OK',[('Content-Type','text/html;charset=utf-8')])
    #返回响应的数据内容
    return file_content

#定义装饰器
def router(url_path):
    def set_func(func):
        def wrapper(*args,**kwargs):
            return func(*args,**kwargs)
        router_dict[url_path]=wrapper
        return wrapper
    return set_func


@router('/other.py')
def other():
    file_content = 'other page ...'
    return file_content

@router('/center.py')
def center():
    path = './templates/center.html'
    with open(path, 'r') as f:
        file_content = f.read()
    row_str = """
                <tr>
                    <td>%s</td>
                    <td>%s</td>
                    <td>%s</td>
                    <td>%s</td>
                    <td>%s</td>
                    <td>%s</td>
                    <td>%s</td>
                    <td>
                        <a type="button" class="btn btn-default btn-xs" href="/update/000426.html"> <span class="glyphicon glyphicon-star" aria-hidden="true"></span> 修改 </a>
                    </td>
                    <td>
                        <input type="button" value="删除" id="toDel" name="toDel" systemidvaule="000426">
                    </td>
                </tr> """
    #连接数据库取得数据
    conn = connect(host='localhost',port=3306,user='root',password='000000',database='stock_db',charset='utf8')
    cur = conn.cursor()
    sql_str = """select info.code,info.short,info.chg,info.turnover,info.price,info.highs,focus.note_info from info inner join focus where info.id = focus.info_id;"""
    cur.execute(sql_str)
    sql_result = cur.fetchall()
    all_data = ''
    for t in sql_result:
        all_data += row_str % (t[0],t[1], t[2], t[3], t[4], t[5], t[6])
    cur.close()
    conn.close()
    
    file_content = re.sub(r'\{%content%\}', all_data, file_content)
    return file_content

@router('/index.py')
def index():
    path = './templates/index.html'
    with open(path, 'r') as f:
        file_content = f.read()
    row_str = """
                    <tr>
                        <td>%s</td>
                        <td>%s</td>
                        <td>%s</td>
                        <td>%s</td>
                        <td>%s</td>
                        <td>%s</td>
                        <td>%s</td>
                        <td>%s</td>
                        <td>
                            <input type="button" value="添加" id="toAdd" name="toAdd" systemidvaule="000007">
                        </td>
                    </tr>  """
    # 连接数据库取得数据
    conn = connect(host='localhost',port=3306,database='stock_db',user='root',password='000000',charset='utf8')
    cur = conn.cursor()
    sql_str = """select * from info;"""
    cur.execute(sql_str)
    sql_result = cur.fetchall()
    
    all_data = ''
    for t in sql_result:
        all_data += row_str % (t[0],t[1],t[2],t[3],t[4],t[5],t[6],t[7])
    cur.close()
    conn.close()
    
    file_content = re.sub(r'\{%content%\}', all_data, file_content)
    return file_content




